/* eslint-disable @typescript-eslint/no-explicit-any */
import { HttpStatusCode, type AxiosResponse } from "axios";

import { ElMessage} from 'element-plus'

//不需要弹出报错响应的url (由各自组件内处理)
const noResponseStatusErrorMsgUrl = ["/auth/do-login"];

export const ResponseStatusHandler = (response : AxiosResponse)=>{
  if(noResponseStatusErrorMsgUrl.indexOf(response.config.url as string | '') != -1){
    return;
  }
  const res = response.data;
  if(res.code && res.code === HttpStatusCode.Ok){
    return;
  }
  if(!res.code){
    ElMessage.error("请求失败")
    return;
  }
  let msg = ""
  switch(res.code){
      case HttpStatusCode.InternalServerError: //500
        msg = res.message ? res.message + "。请重新登录。" : "服务器异常";
      case HttpStatusCode.Unauthorized : //401
        msg = res.message ? res.message + "。请重新登录。" : "TOKEN验证失败或TOKEN过期，重新登录。";
      case HttpStatusCode.Forbidden : //403
        msg = res.message ? res.message + "。请重新登录。" : "权限认证失败，重新登录。";
      case HttpStatusCode.Unauthorized : //404
        msg = res.message ? res.message + "。请重新登录。" : "服务器未找到对应资源";
      default:
        msg = res.message || "服务异常！！！！";
        msg = "【" + res.code + "】" + msg;
  }
  ElMessage.error(msg);
  return;
}
